package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/20/13
 * Time: 9:24 PM
 * To change this template use File | Settings | File Templates.
 */
public class Register {
    private List<Reader> readers;
    private Reader currentReader;

    public Register() {
        this.readers = new ArrayList<Reader>();
        this.currentReader = null;
    }

    public void addReader(Reader reader) {
        readers.add(reader);
    }

    public boolean verify(Reader reader) {
        return getReader(reader) != null;
    }

    public void authenticate(Reader reader){
        if(verify(reader)){
            login(reader);
        }
    }
    public boolean isLogin(Reader reader) {
        if (reader == null || currentReader == null){
            return false;
        }
        return currentReader == getReader(reader);
    }

    private void login(Reader reader){
        currentReader = getReader(reader);
    }

    public Reader currentReader(){
        return currentReader;
    }
    public String reporter(Reader reader){
        Reader registerReader = getReader(reader);
        if (registerReader != null){
            registerReader.reporter();
        }
        return null;
    }

    private Reader getReader(Reader reader) {
        for (Reader registerReader : readers){
            if (registerReader.username().equals(reader.username())){
                if(registerReader.password().equals(reader.password())){
                    return registerReader;
                }
            }
        }
        return null;
    }

    public static Register createRegister(){
        Register register = new Register();
        register.addReader(Reader.createReader());
        return register;
    }

    public boolean verify(String username, String password) {
        return verify(new Reader(username, password));
    }
}
